package cn.cxy.viewpagerindicator

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager

class SimpleViewPagerIndicator(context: Context, attributeSet: AttributeSet? = null) :
    LinearLayout(context, attributeSet) {
    private var mMargin = 3
    private var mCount = 0
    lateinit var mViewPager: ViewPager
    private var views: MutableList<ImageView> = ArrayList()
    private var mSelectedResId: Int
    private var mUnSelectedResId: Int

    init {
        mSelectedResId = R.drawable.ic_selected
        mUnSelectedResId = R.drawable.ic_unselected
    }

    fun setCount(count: Int): SimpleViewPagerIndicator {
        mCount = count
        return this
    }

    fun setViewPager(viewPager: ViewPager): SimpleViewPagerIndicator {
        mViewPager = viewPager
        return this
    }

    fun setSelectedImageResId(imageResId: Int): SimpleViewPagerIndicator {
        mSelectedResId = imageResId
        return this
    }

    fun setUnSelectedImageResId(imageResId: Int): SimpleViewPagerIndicator {
        mUnSelectedResId = imageResId
        return this
    }

    fun setIconMargin(margin: Int): SimpleViewPagerIndicator {
        mMargin = margin
        return this
    }

    fun build() {
        if (mCount > 1) {
            visibility = View.VISIBLE
            initViews(mCount)
            initViewPager(mViewPager)
        } else {
            visibility = View.GONE
        }
    }

    private fun initViews(count: Int) {
        for (i in 0 until count) {
            val view = getView()
            addView(view)
            views.add(view)
        }
        select(0)
    }

    private fun initViewPager(viewPager: ViewPager) {
        mViewPager = viewPager
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                select(position)
            }
        })
    }

    private fun select(position: Int) {
        if (position >= 0 && position <= views.size - 1) {
            views.forEach { it.setImageResource(mUnSelectedResId) }
            views[position].setImageResource(mSelectedResId)
        }
    }

    private fun getView(): ImageView {
        val imageView = ImageView(context)
        imageView.setImageResource(mUnSelectedResId)
        imageView.layoutParams = getLayoutParamsForView()
        return imageView
    }

    private fun getLayoutParamsForView(): LayoutParams {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.setMargins(mMargin, 0, mMargin, 0)
        return layoutParams
    }

}